package com.qf.controller.caiwu_controller;

import com.github.pagehelper.PageInfo;
import com.qf.bean.Debt;
import com.qf.bean.DebtOrder;
import com.qf.service.caiwu_service.DebtOrderService;
import com.qf.service.caiwu_service.DebtService;
import com.qf.utils.caiwu.form.DebtOrderForm;
import com.qf.utils.caiwu.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 2019/12/10 0010
 * Author->DT
 * sassspringboot
 * 行到水穷处 坐看云起时
 * 模块：欠款管理模块
 */
@Controller
@RequestMapping("/debt")
public class deptController {
    @Autowired
    private DebtService debtService;
    @Autowired
    private DebtOrderService debtOrderService;
    //跳转到客户欠款管理页面
    @RequestMapping("/list")
    public String list(@RequestParam(defaultValue = "1")Integer index,
                       Model model,String clientName,String clientChannel){
        //0.封装参数
        Map map=new HashMap();
        map.put("index", index);
        map.put("page", 3);
        map.put("clientName", clientName);
        map.put("clientChannel", clientChannel);
        //1.分页查询欠款信息列表
        PageInfo<Debt> pageInfo = debtService.findDebtListByLimit(map);
        //2.将查询到的信息放入Request域中
        model.addAttribute("pageInfo", pageInfo);
        //3.数据回显信息放入Request域中
        model.addAttribute("clientName",clientName);
        model.addAttribute("clientChannel",clientChannel);
        return "/caiwu/debt-list";
    }

    //跳转到欠还款订单记录页面
    @RequestMapping("/order-list")
    public String orderList(@RequestParam(defaultValue = "1")Integer index,
                            Model model, DebtOrderForm debtOrderForm){
        //0.封装参数
        Map map=new HashMap();
        map.put("index", index);
        map.put("page", 3);
        map.put("form", debtOrderForm);
        //1.分页查询欠还款订单信息列表
        PageInfo<DebtOrder> pageInfo = debtOrderService.findDebtOrderListByLimit(map);
        //2.将查询到的信息放入Request域中
        model.addAttribute("pageInfo", pageInfo);
        //3.数据回显信息放入Request域中
        model.addAttribute("form", debtOrderForm);
        //跳转
        return "/caiwu/debt-order-list";
    }

    //跳转到收欠款页面
    @RequestMapping("/order-add-ui")
    public String OrderAdd(Integer did,Model model){
        //查询当前客户信息
        Debt debt = debtService.selectByPrimaryKey(did);
        //查询当前客户历史欠款单
        List<DebtOrder> debtOrderList=debtOrderService.selectOweDebtOrderList(did);
        //将数据放到请求域中
        model.addAttribute("debt", debt);
        model.addAttribute("debtOrderList", debtOrderList);
        return "/caiwu/debt-order-add";
    }

    //保存收欠款信息
    @RequestMapping("/order-add")
    public String OrderAdd(DebtOrder debtOrder){
        //欠款表中欠款金额减去收款金额
        int count=debtService.subtractOwe(debtOrder.getRefund(),debtOrder.getDid());
        //保存收款订单表
        int count1=debtOrderService.addRefundOrder(debtOrder);
        //重定向到首页
        return "/debt/order-list";
    }

    //跳转欠款提醒页面
    @RequestMapping("/warn-ui")
    public String warnUI(Integer did,Model model){
        //查询当前客户信息
        Debt debt = debtService.selectByPrimaryKey(did);
        model.addAttribute("debt",debt);
        return "/caiwu/debt-warn";
    }

    //欠款提醒
    @RequestMapping("/warn")
    @ResponseBody
    public ResultVO warn(Debt debt){
        int count = debtService.updateByPrimaryKeySelective(debt);
        //返回
        if (count==1){
            return new ResultVO(0, "设置成功！", null);
        }
        return new ResultVO(1, "设置失败！", null);
    }

}
